package other.edan.intlpass.user;
import com.alibaba.fastjson.JSONObject;
import com.rkhd.platform.sdk.ScheduleJob;
import com.rkhd.platform.sdk.data.model.User;
import com.rkhd.platform.sdk.exception.ApiEntityServiceException;
import com.rkhd.platform.sdk.exception.AsyncTaskException;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.param.ScheduleJobParam;
import com.rkhd.platform.sdk.service.FutureTaskService;
import com.rkhd.platform.sdk.service.XoqlService;
import com.rkhd.platform.sdk.task.FutureTask;
import org.apache.commons.lang.StringUtils;
import other.edan.intlpass.utils.HCXObjectService;

import java.util.*;
import java.util.stream.Collectors;


public class UpUserManagerId implements ScheduleJob, FutureTask {
    private static final Logger LOGGER = LoggerFactory.getLogger();
    private static final XoqlService xoqlService = XoqlService.instance();

    /**
     * 触发定时任务
     * @param scheduleJobParam
     */
    @Override
    public void execute(ScheduleJobParam scheduleJobParam) {
        try {
            FutureTaskService.instance().addFutureTask(getClass(), "");
        } catch (AsyncTaskException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 执行异步
     * @param s
     * @throws ScriptBusinessException
     */
    @Override
    public void execute(String s) {
        dealUserManagerId();
    }

    public static void main(String[] args) throws ApiEntityServiceException {
        new UpUserManagerId().dealUserManagerId();
    }

    // 更新用户的直线上级和虚线上级
    private void dealUserManagerId() {
        try {
            LOGGER.info("更新用户的直线上级和虚线上级 ==> 开始处理");

            // 获取所有用户
            String sql = "select id,name,employeeCode,expertise,managerId from user";
            List<JSONObject> userList = xoqlService.query(sql).getRecords();
            Map<String, Long> crmUserMap = userList.stream().collect(Collectors.toMap(e -> e.getString("employeeCode"), e1 -> e1.getLong("id"), (e, e1) -> e));
            LOGGER.info("crmUserMap:" + crmUserMap.size());

            // 获取直接上级
            List<User> updateUserList = new ArrayList<>();
            for (JSONObject user : userList) {
                
                // 上级工号为空或者上级是自己则无需处理
                String expertise = user.getString("expertise");
                String employeeCode = user.getString("employeeCode");
                if (StringUtils.isBlank(expertise) || Objects.equals(expertise, employeeCode)) {
                    continue;
                }

                // 判断是否需要更新 employeeCode：工号  expertise:直属上级工号
                Long newManagerId = crmUserMap.get(expertise);
                Long managerId = user.getLong("managerId");
                if (newManagerId == null || Objects.equals(newManagerId, managerId)) {
                    continue;
                }

                // 修改
                User upUser = new User();
                upUser.setId(user.getLong("id"));
                upUser.setManagerId(newManagerId);
                updateUserList.add(upUser);
            }

            LOGGER.info("更新用户:" + updateUserList);
            if (updateUserList.size() > 0) {
                HCXObjectService.updateBatch(updateUserList);
            }

            LOGGER.info("更新用户的直线上级和虚线上级 处理完成");
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("定时更新用户信息异常, error:", e);
        }
    }


}
